<?php

namespace App\Services\Tools;

use Ethansmart\EsBuilder\Builder\EsClientBuilder;

class EsService
{
    private $client ;
    public function __construct()
    {
        $host = "127.0.0.1";
        $port = "32800";
        $this->client = EsClientBuilder::create()
            ->setHosts($host)
            ->setPort($port)
            ->build();
    }

    public function create($id)
    {
        $data = [
            'index'=>'accounts',
            'type'=>'person',
            'id'=>$id,
            'body'=>[
                'user'=>str_random(6),
                'title'=>str_random(12),
                'desc'=>str_random(16),
            ]
        ];
        try {
            $result =  $this->client->create($data);
        } catch (\Exception $e) {
            return ['code'=>-1, 'msg'=>$e->getMessage()];
        }

        return $result;
    }
}